package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.model.domain.ShopCart;
import com.weixing.mall.provider.model.dto.CartChgNumDto;
import com.weixing.mall.provider.model.dto.ShopCartDto;
import com.weixing.mall.provider.service.IShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @Author wx
 * @Date 2019/9/24 13:45
 * @Version 1.0selectSubmitOrder
 */
@RestController
@RequestMapping("front/cart")
@Api(value = "购物车API",tags = "购物车")
public class FrontCartController extends BaseController {
    @Autowired
    private IShopCartService shopCartService;

    @ApiOperation(value = "加入购物车")
    @PostMapping("save")
    public Result save(@MemInfo MemAuth auth,@RequestBody ShopCartDto shopCartDto){
        shopCartService.save(shopCartDto,auth.getMid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改商品数量")
    @PostMapping("changeItemNum")
    public Result changeNum(CartChgNumDto chgNumDto){
        shopCartService.changeNum(chgNumDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "选中单个商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", value = "购物车商品id", paramType = "path", dataType = "long")
    })
    @PostMapping("selectItem/{itemId}")
    public Result selectItem(@PathVariable Long itemId){
        shopCartService.selectItem(itemId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "选中全部商品")
    @PostMapping("selectAllItem")
    public Result selectAllItem(@MemInfo MemAuth auth){
        shopCartService.selectAllItem(auth.getMid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "取消选中单个商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", value = "购物车商品id", paramType = "path", dataType = "long")
    })
    @PostMapping("cancelItem/{itemId}")
    public Result cancelItem(@PathVariable Long itemId){
        shopCartService.cancelItem(itemId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "取消选中全部商品")
    @PostMapping("cancelAllItem")
    public Result cancelAllItem(@MemInfo MemAuth auth){
        shopCartService.cancelAllItem(auth.getMid());
        return ResultUtil.success();
    }


    @ApiOperation("删除选中的商品")
    @PostMapping("del/{ids}")
    public Result delSingle(@PathVariable String ids){
        if(StrUtil.isNotEmpty(ids)){
            shopCartService.removeByIds(Arrays.asList(ids.split(",")));
        }else{
            return ResultUtil.error("删除对象为空");
        }
        return ResultUtil.success();
    }



    @ApiOperation("清空购物车")
    @PostMapping("delAll")
    public Result delAll(@MemInfo MemAuth auth){
        shopCartService.remove(new QueryWrapper<ShopCart>().eq("member_id",auth.getMid()));
        return ResultUtil.success();
    }


    @ApiOperation("查询购物车")
    @GetMapping("")
    public Result list(@MemInfo MemAuth auth){
        return ResultUtil.success(shopCartService.selectCartListByMemberId(auth.getMid()));
    }
}
